package com.whu.lsj;

import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

/**
 * 1.将测试结果输出
 * 2.将测试结果转化为原始文本
 * @author Enginner-Jie
 */
public class ResultFormat {
	/**
	 * token eg: book B S
	 */
	@Test
	public static void TextFormat(){

		File crf_result = new File(WordSeg.crf_result_file);
		File seg_result = new File(WordSeg.seg_result_file);
		BufferedWriter bw = null;
		BufferedWriter bw2 = null;

		try {
			bw = new BufferedWriter(new FileWriter(crf_result));
			bw2 = new BufferedWriter(new FileWriter(seg_result));
			Iterator<String> it = Command.DOS_text.iterator();
			while(it.hasNext()) {
				String token = it.next();
				bw.write(token+"\n");   //将测试结果文件输出到文本：crf_result

				//将测试结果转换为分词结果输出：seg_result
				if (token.equals("")){
					continue;
				}
				String word = token.substring(0,token.indexOf("\t"));
				char label = token.charAt(token.length() - 1);

				if (label != 'N') {
					if (label == 'S' ||label == 'E' ) {
						bw2.write(word + "\n");
					}else if (label == 'B'||label == 'M'){
						bw2.write(word + " ");
					}else{
					}
				}
				//全局文本输出
//				if (label == 'S') {
//					bw.write("【"+ word + "】"+" ");
//				}else if (label == 'B'){
//					bw.write("【"+ word + " ");
//				}else if (label == 'M'){
//					bw.write(word + " ");
//				}else if (label == 'E'){
//					bw.write(word + "】"+" ");
//				}else {
//					bw.write(word +" ");
//				}
				//只输出术语词
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bw2 != null){
				try {
					bw2.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
